package com.daiqee.controller;

import java.util.ArrayList;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.daiqee.controller.support.BaseController;
import com.daiqee.enums.RequestType;
import com.daiqee.pojo.MenuClassify;
import com.daiqee.pojo.MenuItem;
import com.daiqee.service.impl.MenuClassifyService;
import com.daiqee.service.impl.MenuItemService;
import com.daiqee.vo.MenuVO;

/**
 * 菜单
 * 
 * <br/>
 * @since JDK 1.8
 * @version  V2.4.1
 * @author RichardTang 
 * @date: 2018年6月19日
 * @package com.daiqee.controller
 * @copyright:Copyright (c) 2018, 1245811923@qq.com All Rights Reserved.
 */
@Controller
@RequestMapping("menu")
public class MenuController extends BaseController{

	@Autowired
	private MenuItemService menuItemService;
	
	@Autowired
	private MenuClassifyService menuClassifyService;
	
	@RequestMapping(method = RequestMethod.GET)
	public Object queryMenu() {
		//菜单分类
		List<MenuClassify> menuClassify = menuClassifyService.selectList(null);
		//菜单功能项
		List<MenuItem> menuItem = menuItemService.queryMenuItem();
		//这里必须使用有序Map集合-----将menuClassify 包装成Map方便将数据整合,key为id号,value为对应的英文标识
		Map<Integer,String> classifyMap = new LinkedHashMap<Integer,String>();
		//这个map存储对应的分类功能数据 key为classify value为对应的数据
		Map<String,List<MenuItem>> responseMap = new LinkedHashMap<String,List<MenuItem>>();
		for(MenuClassify itemClassify : menuClassify) {
			classifyMap.put(itemClassify.getId(), itemClassify.getClassify());
			responseMap.put(itemClassify.getClassify(), new ArrayList<MenuItem>());
		}
		//遍历功能项,封装到对应的集合中
		for(MenuItem item : menuItem) {
			//通过classifyMap获取到标识,在通过标识从responseMap中获取到对应的arrayList,来存储MenuItem
			List<MenuItem> toList = responseMap.get(classifyMap.get(item.getClassify()));
			toList.add(item);
		}
		MenuVO vo = new MenuVO();
		vo.setMenuClassify(menuClassify);
		vo.setMenuData(responseMap);
		return restfulResponse.response(vo,RequestType.GET);
	}
}
